package com.exrobot.module.rbt.controller.app;

import cn.hutool.core.collection.CollectionUtil;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;
import com.exrobot.module.rbt.controller.admin.canteeninfo.vo.CanteenInfoPageReqVO;
import com.exrobot.module.rbt.controller.admin.canteeninfo.vo.CanteenInfoRespVO;
import com.exrobot.module.rbt.controller.admin.canteenmenu.vo.CanteenMenuPageReqVO;
import com.exrobot.module.rbt.controller.admin.canteenmenu.vo.CanteenMenuRespVO;
import com.exrobot.module.rbt.controller.admin.canteenmenu.vo.CanteenMenuSimpleRespVO;
import com.exrobot.module.rbt.controller.admin.robotbox.vo.RobotBoxRespVO;
import com.exrobot.module.rbt.dal.dataobject.canteenmenu.CanteenMenuDO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.dal.dataobject.robotbox.RobotBoxDO;
import com.exrobot.module.rbt.dal.redis.RbtRedisDAO;
import com.exrobot.module.rbt.enums.RbtBoxTypeEnum;
import com.exrobot.module.rbt.service.canteeninfo.CanteenInfoService;
import com.exrobot.module.rbt.service.canteenmenu.CanteenMenuService;
import com.exrobot.module.rbt.service.orderitem.OrderItemService;
import com.exrobot.module.rbt.service.robotbox.RobotBoxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

import static com.exrobot.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 食堂信息")
@RestController
@RequestMapping("/robot/foodDelivery")
@Validated
public class FoodDeliveryController {

    @Resource
    private CanteenInfoService canteenInfoService;

    @Resource
    private CanteenMenuService  canteenMenuService;

    @Resource
    private RobotBoxService robotBoxService;

//    @Resource
//    private SaleFoodService saleFoodService;

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private RbtRedisDAO rbtRedisDAO;

    @GetMapping("/getCanteenList")
    @Operation(summary = "获得食堂信息列表")
    @PermitAll
    public CommonResult<List<CanteenInfoRespVO>> getCanteenList() {
        CanteenInfoPageReqVO pageReqVO = new CanteenInfoPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        return success(BeanUtils.toBean(canteenInfoService.getCanteenInfoPage(pageReqVO).getList(),CanteenInfoRespVO.class,item->item.setId(item.getCanteenSn())));
    }

    @GetMapping("/getCanteenMenuList")
    @Operation(summary = "获得食堂菜单列表")
    @PermitAll
    public CommonResult<List<CanteenMenuRespVO>> getCanteenMenuRespList(@RequestParam(value = "canteenId", required = false) Long canteenId) {
        CanteenMenuPageReqVO pageReqVO = new CanteenMenuPageReqVO();
        if (canteenId != null) {
            pageReqVO.setCanteenSn(canteenId);

        }
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        return success(BeanUtils.toBean(canteenMenuService.getCanteenMenuPage(pageReqVO).getList(), CanteenMenuRespVO.class,item->{
            item.setId(item.getMenuSn());
            item.setCanteenId(item.getCanteenSn());
        }));
    }

    @GetMapping("/getRobotBoxList")
    @Operation(summary = "获取餐车柜列表")
    @PermitAll
    public CommonResult<List<RobotBoxRespVO>> getRobotBoxList(@RequestParam(value = "sn",required = false) String sn) {
        return success(BeanUtils.toBean(robotBoxService.getRobotBoxByRobotSn(sn),RobotBoxRespVO.class));
    }

    @GetMapping("/getRobotBoxUseList")
    @Operation(summary = "获得餐食使用列表")
    @PermitAll
    public CommonResult<List<RobotBoxRespVO>> getRobotBoxUseList(@RequestParam(value = "sn", required = true) String sn) {
        List<RobotBoxDO> robotBoxDOList = robotBoxService.getRobotBoxByRobotSn(sn);
        if (CollectionUtil.isEmpty(robotBoxDOList)) {
            return success(new ArrayList<>());
        }
        Map<Integer,OrderItemDO> orderItemDOMap = orderItemService.getOrderItemMap(sn);
        List<Integer> foolErrList = rbtRedisDAO.getLockErr(sn);
        Map<Long, CanteenMenuDO> menuDOMap = new HashMap<>();
        if(CollectionUtil.isNotEmpty(orderItemDOMap)) {
            Collection<Long> menuSns = orderItemDOMap.values().stream().map(OrderItemDO::getMenuSn).collect(Collectors.toSet());
            menuDOMap = canteenMenuService.getCanteenMenuMapByMenuSn(menuSns);
        }
        Map<Long, CanteenMenuDO> finalMenuDOMap = menuDOMap;
        List<RobotBoxRespVO> respVOList = BeanUtils.toBean(robotBoxDOList,RobotBoxRespVO.class, item -> {
            if(foolErrList.contains(item.getBoxNo())) {
                item.setType(RbtBoxTypeEnum.ERR.getTypeVal());
            } else if(orderItemDOMap.containsKey(item.getBoxNo())) {
                OrderItemDO orderItemDO = orderItemDOMap.get(item.getBoxNo());
                item.setType(RbtBoxTypeEnum.FOOD.getTypeVal());
                CanteenMenuSimpleRespVO simpleRespVO = new CanteenMenuSimpleRespVO();
                if(finalMenuDOMap.containsKey(orderItemDO.getMenuSn())) {
                    CanteenMenuDO canteenMenuDO = finalMenuDOMap.get(orderItemDO.getMenuSn());
                    simpleRespVO.setId(orderItemDO.getMenuSn());
                    simpleRespVO.setName(canteenMenuDO.getName());
                    simpleRespVO.setPrice(canteenMenuDO.getPrice());
                    simpleRespVO.setImg(canteenMenuDO.getImg());
                    item.setContent(simpleRespVO);
                }
            } else {
                item.setType(RbtBoxTypeEnum.YES.getTypeVal());
            }
        });
        return success(respVOList);
    }


}